Completed
Push — master ( b745b3...483e7a )
by Equim
01:04
created

co.wrap   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 98

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
nc 10
nop 2
dl 0
loc 98
rs 6.4687
c 1
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A 0 8 2
B 0 35 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
'use strict';
2
3
const
4
    express    = require('express'),
5
    superagent = require('superagent'),
6
    cheerio    = require('cheerio'),
7
    colors     = require('colors'),
0 ignored issues
show
Unused Code introduced by
The constant colors seems to be never used. Consider removing it.
Loading history...
8
    program    = require('commander'),
9
    moment     = require('moment'),
10
    co         = require('co'),
11
    thunkify   = require('thunkify'),
12
    access     = require('./lib/access.js');
13
14
program
15
    .option('-h, --help')
16
    .option('-v, --version')
17
    .option('-p, --port [value]', parseInt)
0 ignored issues
show
Bug introduced by
The variable parseInt seems to be never declared. If this is a global, consider adding a /** global: parseInt */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
18
    .option('-f, --fullLog')
19
    .parse(process.argv);
20
21
// 别问我为什么这里逻辑这么奇怪……测试的结果确实是这样的啊hhh
22
if (!program.help || !program.version) {
23
    console.log(
24
`${`CSUEMS API v${require('./package').version}
25
by The Liberators`.rainbow}${program.help ? '' :
26
`
27
28
Preparation:
29
  ${'(This section is WIP)'.grey}
30
31
Usage:
32
  npm start [-- <options...>]
33
34
Options:
35
  -h, --help          print this message and exit.
36
  -v, --version       print the version and exit.
37
  -f, --fullLog       enable full log, by default only errors are logged.
38
  -p, --port [value]  specify a port to listen, process.env.PORT || 2333 by default.
39
40
Examples:
41
  $ npm start -- -p 43715               # listening to 43715
42
  $ forever start app.js                # deploy with forever as daemon (root access recommended)
43
  $ pm2 start -i 0 -n "csuapi" app.js   # deploy with pm2 as daemon  (root access recommended)`}`);
44
    process.exit(0);
45
}
46
47
superagent.Request.prototype.endThunk = thunkify(superagent.Request.prototype.end);
48
49
const
50
    logging = (log) => console.log(`${moment().format('[[]YY-MM-DD HH:mm:ss[]]')} ${log}`),
51
    fullLogging = (log) => program.fullLog && logging(log),
52
    getSem = () => {
53
        // 以系统时间获取当前学期
54
        let now = new Date();
55
        let month = now.getMonth();
56
        let year = now.getFullYear();
57
        if (month <= 6) {
58
            return `${year - 1}-${year}-${month > 0 ? 2 : 1}`;
59
        }
60
        return `${year}-${year + 1}-1`;
61
    },
62
//  wait = (ms) => (callback) => setTimeout(callback, ms),
63
    port = program.port || process.env.PORT || 2333,
64
    app = express();
65
66
// 获取文档
67
app.get('/doc', (req, res) => res.sendFile(`${__dirname}/doc/API.html`));
68
69
// 查成绩API,通过GET传入用户名和密码
70
app.get(/^\/g(?:|rades)$/, co.wrap(function *(req, res) {
71
    if (!req.query.id || !req.query.pwd || (req.query.sem && !(/^20\d{2}-20\d{2}-[1-2]$/).test(req.query.sem))) {
72
        res.status(404).send({ error: "参数不正确" });
73
        return;
74
    }
75
76
    let start = new Date();
77
    fullLogging('Started to query the grades: '.cyan + req.query.id.yellow);
78
79
    let headers;
80
    try {
81
        headers = yield access.login(req.query.id, req.query.pwd);
82
    } catch (err) {
83
        logging(err.eqMessage.inner.red);
84
        res.status(404).send({ error: err.eqMessage.public });
85
        return;
86
    }
87
    fullLogging('Successfully logged in.'.green);
88
89
    let ires;
90
    try {
91
        // 实际上xnxq01id为空的时候和GET这个URL的效果是一样的,都是查询所有学期
92
        ires = yield superagent
93
            .post('ttp://csujwc.its.csu.edu.cn/jsxsd/kscj/yscjcx_list')
94
            .set(headers)
95
            .type('form')
96
            .send({ xnxq01id: req.query.sem })
97
            .endThunk();
98
    } catch (err) {
99
        logging(`Failed to get grades page\n${err.stack}`.red);
100
        res.status(404).send({ error: '无法进入成绩页面' });
101
    } finally {
102
        // 直接异步进行?
103
        co(function *() {
104
            try {
105
                yield access.logout(headers);
106
                fullLogging('Successfully logged out: '.green + req.query.id.yellow);
107
            } catch (err) {
108
                logging(err.eqMessage.inner.red);
109
            }
110
        });
111
    }
112
    fullLogging('Successfully entered grades page.'.green);
113
114
    let $ = cheerio.load(ires.text);
115
116
    let top = $('#Top1_divLoginName').text();
117
    let result = {
118
        name: top.match(/\s.+\(/)[0].replace(/\s|\(/g, ''),
119
        id: top.match(/\(.+\)/)[0].replace(/\(|\)/g, ''),
120
        grades: {},
121
        'subject-count': 0,
122
        failed: {},
123
        'failed-count': 0,
124
    };
125
    // 获取成绩列表
126
    $('#dataList tr').each(function (index) {
127
        if (index === 0) {
128
            return;
129
        }
130
131
        let element = $(this).find('td');
132
133
        let title = element.eq(3).text().match(/].+$/)[0].substring(1);
134
        let item = {
135
            sem: element.eq(2).text(),
136
            reg: element.eq(4).text(),
137
            exam: element.eq(5).text(),
138
            overall: element.eq(6).text()
139
        };
140
        if (req.query.details) {
141
            item.id = element.eq(3).text().match(/\[.+\]/)[0].replace(/\[|\]/g, '');
142
            item.attr = element.eq(8).text();
143
            item.genre = element.eq(9).text();
144
            item.credit = element.eq(7).text();
145
        }
146
147
        // 如果有补考记录,则以最高分的为准(暂不考虑NaN)
148
        if (title in result.grades && item.overall < result.grades[title].overall) {
149
            return;
150
        }
151
152
        result.grades[title] = item;
153
154
        // 挂科判定
155
        if (element.eq(6).css('color')) {
156
            result.failed[title] = item;
157
        } else {
158
            delete result.failed[title];
159
        }
160
    });
161
162
    result['subject-count'] = Object.keys(result.grades).length;
163
    result['failed-count'] = Object.keys(result.failed).length;
164
165
    res.send(JSON.stringify(result));
166
    fullLogging(`Successfully responded. (req -> res processed in ${new Date() - start} ms)`.green);
167
}));
168
169
// 查考试API,通过GET传入用户名和密码
170
app.get(/^\/e(?:|xams)$/, co.wrap(function *(req, res) {
171
    if (!req.query.id || !req.query.pwd || (req.query.sem && !(/^20\d{2}-20\d{2}-[1-2]$/).test(req.query.sem))) {
172
        res.status(404).send({ error: "参数不正确" });
173
        return;
174
    }
175
176
    let start = new Date();
177
    fullLogging('Started to query the exams: '.cyan + req.query.id.yellow);
178
179
    let headers;
180
    try {
181
        headers = yield access.login(req.query.id, req.query.pwd);
182
    } catch (err) {
183
        logging(err.eqMessage.inner.red);
184
        res.status(404).send({ error: err.eqMessage.public });
185
        return;
186
    }
187
    fullLogging('Successfully logged in.'.green);
188
189
    let ires;
190
    let _sem = req.query.sem || getSem();
191
    try {
192
        ires = yield superagent
193
            .post('http://csujwc.its.csu.edu.cn/jsxsd/xsks/xsksap_list')
194
            .set(headers)
195
            .type('form')
196
            .send({
197
                xqlbmc: '',
198
                xnxqid: _sem,
199
                xqlb: ''
200
            })
201
            .endThunk();
202
    } catch (err) {
203
        logging(`Failed to reach exams page\n${err.stack}`.red);
204
        res.status(404).send({ error: '无法进入考试页面' });
205
        return;
206
    } finally {
207
        co(function *() {
208
            try {
209
                yield access.logout(headers);
210
            } catch (err) {
211
                logging(err.eqMessage.inner.red);
212
            }
213
            fullLogging('Successfully logged out: '.green + req.query.id.yellow);
214
        });
215
    }
216
    fullLogging('Successfully entered exams page.'.green);
217
218
    let $ = cheerio.load(ires.text);
219
220
    let top = $('#Top1_divLoginName').text();
221
    let result = {
222
        name: top.match(/\s.+\(/)[0].replace(/\s|\(/g, ''),
223
        id: top.match(/\(.+\)/)[0].replace(/\(|\)/g, ''),
224
        sem: _sem,
225
        exams: {},
226
        'exams-count': 0,
227
    };
228
    $('#dataList tr').each(function (index) {
229
        if (index === 0) {
230
            return;
231
        }
232
233
        let element = $(this).find('td');
234
235
        let title = element.eq(3).text();
236
        let item = {
237
            time: element.eq(4).text(),
238
            location: element.eq(5).text(),
239
            seat: element.eq(6).text()
240
        };
241
242
        result.exams[title] = item;
243
        result['exams-count']++;
244
    });
245
246
    res.send(JSON.stringify(result));
247
    fullLogging(`Successfully responded. (req -> res processed in ${new Date() - start} ms)`.green);
248
}));
249
250
app.listen(port, () => {
251
    logging(`The API is now running on port ${port}. Full logging is ${program.fullLog ? 'enabled' : 'disabled'}`.green);
252
});
253